//
//  UIButton+YButton.m
//  WidgetTest
//
//  Created by 李明明 on 15/10/10.
//  Copyright © 2015年 李明明. All rights reserved.
//

#import "UIButton+YButton.h"
#import <objc/runtime.h>

static const char *UIButton_acceptEventInterval = "UIControl_acceptEventInterval";

@implementation UIButton (YButton)
- (CGFloat)timeInvertal
{
    return [objc_getAssociatedObject(self, UIButton_acceptEventInterval) floatValue];
}
- (void)test
{
    NSLog(@"%@", @"test");
}
- (void)setTimeInvertal:(CGFloat)timeInvertal
{
    objc_setAssociatedObject(self, UIButton_acceptEventInterval, @(timeInvertal), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event
{
    if (self.timeInvertal > 0) {
        NSDate *date = [NSDate date];
        // 距上一次点击的时间间隔 如果为0说明是第一次点击
        static NSTimeInterval time = 0;
        // 上一次点击的时间 如果为0说明是第一次点击
        static NSTimeInterval beforeClickTime = 0;
        // 判断如果不是第一次点击 就给time重新赋值
        if (beforeClickTime != 0) {
            time = date.timeIntervalSince1970 - beforeClickTime;
        }
        // 更新beforeClickTime 把本次点击作为上一次点击的时间
        beforeClickTime = date.timeIntervalSince1970;
        // 如果time小于设定的时间间隔 则本次点击作废
        if (time < self.timeInvertal && time != 0) {
            return;
        }
    }
    [super sendAction:action to:target forEvent:event];
}
@end
